import { _decorator, AudioClip, Component, Label, labelAssembler, Node, Prefab } from 'cc';
import { Player } from './Player';
import { AudioMgr } from './AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('GameMgr')
export class GameMgr extends Component {

    @property([Prefab])
    spikes:Prefab[]=[];

    static ins: GameMgr = null
    @property(Node)
    failPanel: Node;
    @property(Node)
    startNode: Node;
    @property(Label)
    scoreLabel: Label = null;
    @property(Label)
    bestScoreLabel: Label
    @property(Node)
    newNode: Node;
    @property([AudioClip])
    audioClips:AudioClip[]=[]

    private playerScore:number=0;

    set PlayerScore(value) {
        this.playerScore = value;
        this.scoreLabel.string = value + "";
    }
    get PlayerScore() {
        return this.playerScore;
    }


    hightScore:number=0;

    private isGameIng=false;

    get IsGameIng() {
        return this.isGameIng;
    }
    set IsGameIng(value) {
        this.isGameIng=value;

        if (value==true) {
            this.startNode.active=false;
        }
    }

    jumpNum=0;

    protected onLoad(): void {
        GameMgr.ins=this;
    }

    start() {
        //获取缓存
        if (localStorage.getItem("userInfo")) {
            let userInfo=JSON.parse(localStorage.getItem("userInfo"));
            this.hightScore=userInfo.hightScore;
        }
        else{
            let data={
                hightScore:0
            }
            localStorage.setItem("userInfo",JSON.stringify(data))

        }

        AudioMgr.ins.play(this.audioClips[0],0.3);
        this.node.on(Node.EventType.TOUCH_START, this.touchStartFuc, this)
    }

    touchStartFuc(){
        AudioMgr.ins.playOneShot(this.audioClips[1]);

        if (this.IsGameIng==false) {
            this.IsGameIng=true;
            return;
        }

        if (this.jumpNum==2) {
            return;
        }
        Player.ins.jump();
        this.jumpNum += 1
    }

    onclickFailPanelComfrimBtn() {
        this.failPanel.active = false;
        this.startNode.active=true;
        GameMgr.ins.PlayerScore=0;

        //还原环境
        this.node.getChildByName("bg").getChildByName("environment0").getChildByName("land").removeAllChildren();
        this.node.getChildByName("bg").getChildByName("environment1").getChildByName("land").removeAllChildren();

        this.node.getChildByName("bg").getChildByName("environment0").setPosition(0,0,0)
        this.node.getChildByName("bg").getChildByName("environment1").setPosition(750,0,0)

        this.node.getChildByName("bg").getChildByName("player").setPosition(0,-490,0)
    }

    gameOver() {
        AudioMgr.ins.playOneShot(this.audioClips[2]);

        this.IsGameIng = false;
        this.failPanel.active = true;
        this.failPanel.getChildByName("bg").getChildByName("scoreLabel").getComponent(Label).string = this.PlayerScore + "";
        this.failPanel.getChildByName("bg").getChildByName("bestScoreLabel").getComponent(Label).string = this.hightScore + "";
        if (this.PlayerScore > this.hightScore) {
            this.hightScore = this.PlayerScore
            this.newNode.active = true;
        }
        else{
            this.newNode.active = false;
        }
        GameMgr.ins.saveData();

    }

    saveData() {
        let data = {
            hightScore: this.hightScore
        }
        localStorage.setItem("userInfo", JSON.stringify(data))
    }


    randowNum(min: number, max: number) {

        let ret = Math.floor(Math.random() * (max - min + 1)) + min;

        return ret;
    }




}


